package com.persondemo.videoappliction.ui.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.utils.DensityUtil;
import com.persondemo.videoappliction.utils.ImageLoader;
import com.persondemo.videoappliction.utils.ImageLoaderUtil;


public class GridAdapter extends BaseAdapter {

	private List<String> paths;
	private Context mContext;
	private ImageLoader imageLoader;
	public GridAdapter(List<String> paths, Context mContext) {
		super();
		this.paths = paths;
		this.mContext = mContext;
		
		imageLoader = ImageLoader.getInstance(mContext);
	}

	@Override
	public int getCount() {
		
		return this.paths.size();
	}

	@Override
	public Object getItem(int position) {
		return this.paths.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder=null;
		if(convertView==null){
			convertView = LayoutInflater.from(this.mContext).inflate(R.layout.item_imgs, null);
			holder = new ViewHolder();
			holder.photo=(ImageView)convertView.findViewById(R.id.detils_item_img);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder)convertView.getTag();
		}
		
		final String path = this.paths.get(position);
		holder.photo.setImageResource(R.drawable.icon);
		holder.photo.setTag(path);
		ViewHolder finalHolder1 = holder;
		imageLoader.loadImage(holder.photo,
				path, 
				DensityUtil.dip2px(80),
				DensityUtil.dip2px(80), 
				new ImageLoader.onBitmapLoadedListener() {
			@Override
			public void displayImage(ImageView view, Bitmap bitmap) {
				String imagePath= view.getTag().toString();
				if(imagePath.equals(path)){
					ImageLoaderUtil.LoadImage(mContext,bitmap, finalHolder1.photo);
				}
			}
		});
		return convertView;
	}
	
	public static class ViewHolder{
		public ImageView photo;
	}

	
}
